package com.hanrui.android.fragment.message.audit.askforleave;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.hanrui.android.R;
import com.hanrui.android.activity.TermActivity;
import com.hanrui.android.library.base.BaseFragment;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.GetNextActvityBean;
import com.hanrui.android.net.contract.message.audit.askforleave.AskForLeaveBizContract;
import com.hanrui.android.net.presenter.message.audit.askforleave.AskForLeaveBizPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 请假用品审批流程
 */

public class AskForLeaveBizFragment extends BaseFragment<AskForLeaveBizPresenter> implements AskForLeaveBizContract.View {

    @BindView(R.id.next)
    LinearLayout nextLl;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.name_go)
    ImageView nameGo;
    @BindView(R.id.bm_ll)
    LinearLayout bmLl;
    @BindView(R.id.bm)
    TextView bm;
    @BindView(R.id.bm_go)
    ImageView bmGo;
    @BindView(R.id.reson_ll)
    LinearLayout resonLl;
    @BindView(R.id.reson)
    EditText reson;

    String taskId,procInsId,bizId;
    String transitionId = "";
    String nextAssigneeList = "";

    OptionsPickerView pvOptions;

    ArrayList<GetNextActvityBean.NextActivityBean.NextAssigneeListBean> dataList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_chapterbiz;
    }

    @Override
    protected void initInjector() { mFragmentComponent.inject(this); }

    @Override
    protected void initView(View view) {

        taskId = getArguments().getString("taskId");
        procInsId = getArguments().getString("procInsId");
        bizId = getArguments().getString("bizId");

        mPresenter.getNextActivity(getActivity(),taskId,procInsId,bizId);

    }

    //实例化
    public static AskForLeaveBizFragment newInstance(String taskId, String procInsId, String bizId) {
        AskForLeaveBizFragment fragment = new AskForLeaveBizFragment();
        Bundle bundle = new Bundle();
        bundle.putString("taskId", taskId);
        bundle.putString("procInsId", procInsId);
        bundle.putString("bizId", bizId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void getNextActivity(GetNextActvityBean data) {

        name.setText(data.getNextActivity().getName());
        final GetNextActvityBean.NextActivityBean bean = data.getNextActivity();

        if (bean.getType().equals("endEvent")) {

            bmLl.setVisibility(View.GONE);
            resonLl.setVisibility(View.GONE);
            reson.setText("");
            return;

        		}

        if (null != bean.getTransitions() && bean.getTransitions().size() > 0) {

            List<GetNextActvityBean.NextActivityBean.TransitionsListBean> transitionsList = bean.getTransitions();

            name.setText(transitionsList.get(0).getName());
            transitionId = transitionsList.get(0).getId();

            if (null != transitionsList.get(0).getNextAssigneeList() && transitionsList.get(0).getNextAssigneeList().size() >0) {

                bm.setText(transitionsList.get(0).getNextAssigneeList().get(0).getName());
                nextAssigneeList = transitionsList.get(0).getNextAssigneeList().get(0).getId();

                getTransitionNextActivity(transitionsList.get(0).getNextAssigneeList());

            }else {

                bmLl.setVisibility(View.GONE);
                nextAssigneeList = "";

            }

            if (transitionsList.size() == 1) {

                name.setText(bean.getName());
                nextLl.setEnabled(false);
                nameGo.setVisibility(View.GONE);


            		}else {

                nextLl.setEnabled(true);
                nameGo.setVisibility(View.VISIBLE);

                pvOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        transitionId = bean.getTransitions().get(options1).getId();
                        name.setText(bean.getTransitions().get(options1).getPickerViewText());
                        if (null != bean.getTransitions().get(options1).getNextAssigneeList() &&
                                bean.getTransitions().get(options1).getNextAssigneeList().size() > 0) {

                            bmLl.setVisibility(View.VISIBLE);
                            bm.setText(bean.getTransitions().get(options1).getNextAssigneeList().get(0).getName());
                            nextAssigneeList = bean.getTransitions().get(options1).getNextAssigneeList().get(0).getId();
                            getTransitionNextActivity(bean.getTransitions().get(options1).getNextAssigneeList());

                        }else {

                            bmLl.setVisibility(View.GONE);
                            bm.setText("");
                            bm.setHint("请选择");
                            nextAssigneeList = "";
                        }

                    }
                }).build();
                pvOptions.setPicker(bean.getTransitions());

            }

        		}else {


            if (null != bean.getNextAssigneeList() && bean.getNextAssigneeList().size() > 0) {

                nextAssigneeList = bean.getNextAssigneeList().get(0).getId();
                bm.setText(bean.getNextAssigneeList().get(0).getName());
                getTransitionNextActivity(bean.getNextAssigneeList());

            }else {

                bmLl.setVisibility(View.GONE);
                nextAssigneeList = "";
            }



        }

    }


    private void getTransitionNextActivity(List<GetNextActvityBean.NextActivityBean.NextAssigneeListBean> nextAssigneeList) {

        if (nextAssigneeList.size() == 1) {

            bmLl.setEnabled(false);
            bmGo.setVisibility(View.GONE);

        }else {

            bmLl.setEnabled(true);
            bmGo.setVisibility(View.VISIBLE);
            dataList.clear();
            dataList.addAll(nextAssigneeList);
            bmLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    TermActivity.start(AskForLeaveBizFragment.this, Constant.nextactivity,dataList,false);
                }
            });

        }

    }

    @Override
    public void askForLeaveBizApproval(int code) {
        if (code == 1) {

            ToastUtils.showShort("提交成功");
            getActivity().finish();

        		}else {

            ToastUtils.showShort("提交失败");
        }

    }


    @OnClick({R.id.next_ll,R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.next_ll:
                if (null != pvOptions)
                    pvOptions.show();
                break;
            case R.id.login_out:
                mPresenter.askForLeaveBizApproval(getActivity(),nextAssigneeList,taskId,procInsId,bizId,transitionId,reson.getText().toString());
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Constant.nextactivity:
                if (null != data) {
                    String result = data.getStringExtra("data");
                    String countING = data.getStringExtra("id");
                    if (null != result && !"".equals(result)) {
                        bm.setText(result);
                    }
                    nextAssigneeList = countING;
                }
                break;
        }
    }
}
